#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using System.IO;
using System;
#endregion

namespace AchievementTestGame
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        #region Initialization


        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base("Main Menu")
        {
            // Create our menu entries.
            MenuEntry playGameMenuEntry = new MenuEntry("Play Game");
            MenuEntry achievementsMenuEntry = new MenuEntry("Display Achievements");
            MenuEntry writeAchievmentsMenuEntry = new MenuEntry("Save achievements");
            MenuEntry readAchievemetsMenuEntry = new MenuEntry("Load achievements");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");

            // Hook up menu event handlers.
            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            achievementsMenuEntry.Selected += AchievementsMenuEntrySelected;
            writeAchievmentsMenuEntry.Selected += writeAchievmentsMenuEntrySelected;
            readAchievemetsMenuEntry.Selected += readAchievmentsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(playGameMenuEntry);
            MenuEntries.Add(achievementsMenuEntry);
            MenuEntries.Add(writeAchievmentsMenuEntry);
            MenuEntries.Add(readAchievemetsMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new UnlockGameScreen(), e.PlayerIndex);
        }


        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void AchievementsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new DisplayAchievementsScreen(), e.PlayerIndex);
        }

        void writeAchievmentsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            using (FileStream fs = new FileStream(Path.Combine(Environment.CurrentDirectory, "achievements.bin"), FileMode.Create))
            {
                //Write the complete achievement data to a file calles "achievements.bin"
                //You will probably do this, when your game starts
                ScreenManager.AchievementManager.WriteAllToStream(fs);
            }
        }

        void readAchievmentsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            using (FileStream fs = new FileStream(Path.Combine(Environment.CurrentDirectory, "achievements.bin"), FileMode.Open))
            {
                //Read that file back
                //You will probably to this, when the game terminates (or better: just before ;))
                ScreenManager.AchievementManager.ReadAllFromStream(fs);
            }
        }

        protected override void OnCancel(PlayerIndex playerIndex)
        {
            ScreenManager.Game.Exit();
            base.OnCancel(playerIndex);
        }

        #endregion
    }
}
